$(function() {
$('#city_5').css("display", "block");
	var id = getvl("id");
	if(id != '') {
    //地址
		$('#city_5').css("display", "none");
		$('#address12').css("display", "block");
		$.ajax({
			url: urlcore + "/api/address/selectOne?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					var da = data.data;
				    $('#userNames').val(da.userName);
					$('#address').val(da.address);
					$('#userPhone').val(da.userPhone);
					$('#postalCode').val(da.postalCode);
					if(da.level==1){
						$(".shopCheck").attr("checked",true);
					}else{
						$(".shopCheck").prop("checked",false);
					}
					var addressSpan = da.province + "/" + da.city + "/" + da.county;
					$('#addressSpan').text(addressSpan);

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					if(thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href = "login.html";
					}
				} else {
					layer.alert(data.msg);
				}
			},
			error: function() {
				layer.alert("error");
			}
		});
	}

});
var currentPage = 1;
//列表
loadMyEssay();

function loadMyEssay() {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$('#addressList').html("");
		$.ajax({
			url: urlcore + "/api/address/selectAddressAll1?current=" + pageNo,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						
						var province=n.province==null?"":n.province;
						var city=n.city==null?"":n.city;
						var county=n.county==null?"":n.county;
						var address=n.address==null?"":n.address;
						
						var postalCode=n.postalCode==null?'暂无':n.postalCode;

						var addressList =
							'<tr>' +
							'<td>' + n.userName + '</td>' +
							'<td>' + province + '' + city + '' + county + '</td>' +
							'<td>' + province + '' + city + '' + county + '' + address + '</td>' +
							'<td>' + postalCode + '</td>' +
							'<td>' + n.userPhone + '</td>' +
							'<td class="or"><a href="24.0.0wode_dizhi.html?id=' + n.id + '"><span style="color:orange;">修改</span></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:delAddress(' + n.id + ');"><span style="color:orange;">删除</span></a></td>' +
							'</tr>';
						$('#addressList').append(addressList);
					});
					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}
function getAdress(val) {
	if(val == 1) {
		$('#city_5').css("display", "block");
		$('.fanhui').css("display", "block");
		$('#address12').css("display", "none");
	} else {
		$('#city_5').css("display", "none");
		$('#address12').css("display", "block");
	}
}

//添加
function subMsg() {
	var userName = $("#userNames").val();
	if(userName == '') {
		layer.alert("用户名不能为空！");
		return false;
	}

	var city_5 = document.getElementById("city_5");
	if(city_5.style.display == 'block') {
		var pro = $("#province").val();
		var ct = $("#city").val();
		var co = $("#county").val();
		if(pro == -1 || ct == -1 || co == -1) {
			alert("请把省或市或区填写完整！");
			return false;
		} else {
			var province = $("#province option:selected").text();
			var city = $("#city option:selected").text();
			var county = $("#county option:selected").text();
			/*alert(province);
			alert(city);
			alert(county);*/
		}
	}
	var address = $("#address").val();
	if(address == '') {
		layer.alert("详细地址不能为空！");
		return false;
	}
	var userPhone = $('#userPhone').val();
	if(userPhone == '') {
		layer.alert("手机号码不能为空！");
		return false;
	}
	var postalCode = $('#postalCode').val();
	if(postalCode == '') {
		layer.alert("邮政编码不能为空！");
		return false;
	}
	if($('.shopCheck').prop('checked')) { //如果按钮被选中
		var level = 1;
	} else {
		var level = 0;
	}
	var id = getvl("id");
	var url = '';
	if(id == '') {
		url = '/api/address/add';
	} else {
		url = '/api/address/update';
	}

	$.ajax({
		url: urlcore + url,
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"id": id,
			"userName": userName,
			"userPhone": userPhone,
			"province": province,
			"city": city,
			"county": county,
			"address": address,
			"postalCode": postalCode,
			"level": level

		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				if(id == "") {
					if(confirm("添加成功，是否继续添加？")) {
						//刷新
						location.reload();
					} else {
						//跳转列表页
						window.location.href = "12.2.0shouhuodizhi.html";
					}
				} else {
					layer.alert("修改成功");
					//跳转列表页
					window.location.href = "12.2.0shouhuodizhi.html";
				}
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}

		},
		error: function() {
			layer.alert("error");
		}

	});
}

//删除地址
function delAddress(id) {
	if(!layer.confirm("确认删除吗?")) {
		return;
	}
	$.ajax({
		url: urlcore + '/api/address/del',
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"id": id,
			"status": 2
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				layer.alert("删除成功");
				//跳转列表页
				window.location.href = "12.2.0shouhuodizhi.html";
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}

		},
		error: function() {
			layer.alert("error");
		}

	});
}